package com.example.wyc.mybiye1;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wyc.mybiye.R;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private TextView txt_channel, txt_message, txt_better, txt_setting;
    private FrameLayout ly_content;
    public Ma1Activity ma1Activity;
    public Ma2Activity ma2Activity;
    public Ma3Activity ma3Activity;
    public Ma4Activity ma4Activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        txt_channel.performClick();   //模拟一次点击，既进去后选择第一项

    }

    public void initView() {
        settTextView();
        txt_channel.setOnClickListener(this);
        txt_message.setOnClickListener(this);
        txt_better.setOnClickListener(this);
        txt_setting.setOnClickListener(this);
        ly_content = (FrameLayout) findViewById(R.id.ly_content);
    }
    public void settTextView() {
        txt_channel = findViewById(R.id.txt_channel);
//        Drawable[] drawable = txt_channel.getCompoundDrawables();
//        // 数组下表0~3,依次是:左上右下
//        drawable[1].setBounds(0, 0, 160, 120);
//        txt_channel.setCompoundDrawables(null, drawable[1], null,
//                null);
        txt_message = findViewById(R.id.txt_message);
//        Drawable[] drawable1 = txt_message.getCompoundDrawables();
//        // 数组下表0~3,依次是:左上右下
//        drawable1[1].setBounds(0, 0, 160, 120);
//        txt_message.setCompoundDrawables(null, drawable1[1], null,
//                null);

        txt_better = findViewById(R.id.txt_better);
//        Drawable[] drawable3 = txt_better.getCompoundDrawables();
//        // 数组下表0~3,依次是:左上右下
//        drawable3[1].setBounds(0, 0, 160, 120);
//        txt_better.setCompoundDrawables(null, drawable3[1], null,
//                null);

        txt_setting = findViewById(R.id.txt_setting);
//        Drawable[] drawable4 = txt_setting.getCompoundDrawables();
//        // 数组下表0~3,依次是:左上右下
//        drawable4[1].setBounds(0, 0, 160, 120);
//        txt_setting.setCompoundDrawables(null, drawable4[1], null,
//                null);
    }

    //重置所有文本的选中状态
    private void setSelected() {
        txt_channel.setSelected(false);
        txt_message.setSelected(false);
        txt_better.setSelected(false);
        txt_setting.setSelected(false);
    }

    private WebView webView;
    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.txt_channel:
                setSelected();
                txt_channel.setSelected(true);
                removeAllView();
                if (ma1Activity == null) {
                    ma1Activity=new Ma1Activity(this);
                    ly_content.addView(ma1Activity.getView());
                } else {
                    ma1Activity.getView();
                }
                ma1Activity.showView();
                webView=ma1Activity.showWeb();
                break;
            case R.id.txt_message:
                //webView=null;
                setSelected();
                txt_message.setSelected(true);
                removeAllView();
                if (ma2Activity == null) {
                    ma2Activity = new Ma2Activity(this,this);
                    ly_content.addView(ma2Activity.getView());
                } else {
                    ma2Activity.getView();
                }
                ma2Activity.showView();
                break;
            case R.id.txt_better:
                //webView=null;
                setSelected();
                txt_better.setSelected(true);
                removeAllView();
                if (ma3Activity==null){
                    ma3Activity=new Ma3Activity(this,this);
                    ly_content.addView(ma3Activity.getView());
                }else {
                    ma3Activity.getView();
                }
                ma3Activity.showView();
                break;
            case R.id.txt_setting:
                //webView=null;
                setSelected();
                txt_setting.setSelected(true);
                removeAllView();
                if (ma4Activity == null) {
                    ma4Activity=new Ma4Activity(this,this);
                    ly_content.addView(ma4Activity.getView());
                } else {
                    ma4Activity.getView();
                }
                ma4Activity.showView();
                break;
        }
    }

    /**
     * 移除不需要的视图
     */
    private void removeAllView() {
        for (int i = 0; i < ly_content.getChildCount(); i++) {
            ly_content.getChildAt(i).setVisibility(View.GONE);
        }
    }
    private long exitTime = 0;
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                super.onBackPressed();
                ma2Activity.lightSensorManager.unRegisterSensor();
            }

        }
    }
}
